package com.zhuss.food.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.order.wechat.TopicOrderBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.dao.ext.OrderDao;
import com.zhuss.food.db.dao.ext.OrderProductDao;
import com.zhuss.food.db.entity.Order;
import com.zhuss.util.JsonUtil;
/**
 * 
 * @author zhushanshuo
 * @since 2020.03.20 09:50
 *
 */

@Component("orderTopic")
public class OrderTopic extends AbstractProcessor<Object, TopicOrderBo> implements Processor<Object, TopicOrderBo> {
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private OrderProductDao orderProductDao;
	
	@Autowired
	private OrderCheck orderCheck;
	
	@Override
	public Object process(TopicOrderBo bo) throws WpsException {
		
		Long orderId = bo.getOrderId();
		Long userId = bo.getConcurrentUserId();
		
		// 当前订单不满足评价条件
		if (!orderCheck.checkTopic(orderId)) {
			throw new WpsException(ExceptionCode.当前订单不满足评价条件);
		}
		
		// 每个用户只能评价自己的订单
		if (!orderCheck.checkBelongUser(orderId, userId)) {
			throw new WpsException(ExceptionCode.每个用户只能评价自己的订单);
		}
		
		Order order = new Order();
		order.setOrderId(bo.getOrderId());
		order.setStatus(OrderStatus.已评价.key());
		
		order.setModifierId(bo.getConcurrentUserId());
		order.setModifier(bo.getConcurrentUserName());
		order.setModifyTime(new Date());
		orderDao.updateByPrimaryKeySelective(order);
		
		orderProductDao.topic(orderId, OrderStatus.已评价.key());
		return null;
	}

	@Override
	public Object log(TopicOrderBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.餐饮.key());
    	record.setAction(LogActionEnum.订单评价.key());
    	record.setTargetId(param.getOrderId());
    	record.setTargetName(param.getOrderId() + "");
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}

	@Override
	public void check(TopicOrderBo param) throws WpsException {
		// TODO Auto-generated method stub
		
	}

}
